package com.yc.ycpaimon.notification

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.content.Intent
import android.provider.Settings
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import com.yc.ycpaimon.App
import com.yc.ycpaimon.R


/**
 * Creator: yc
 * Date: 2022/3/14 16:33
 * UseDes:下载通知
 */
object NotificationUtil {
    const val CHANNEL_ID_DOWNLOAD = "channel_id_download"
    const val PUSH_ID_DOWNLOAD = 1001

    @JvmStatic
    fun showDownload(onProgress:(progress:Float)->Unit) {
//        val intent = Intent(this, AlertDetails::class.java).apply {
//            flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
//        }
//        val pendingIntent: PendingIntent = PendingIntent.getActivity(App.mInstance, 0, intent, 0)
        val notificationManager = NotificationManagerCompat.from(App.mInstance)
        notificationManager.createNotificationChannel(createNotificationChannel())
        val builder = NotificationCompat.Builder(App.mInstance, CHANNEL_ID_DOWNLOAD).setSmallIcon(R.drawable.logo).setContentTitle("My notification")
            .setContentText("Hello World!")
            // .setStyle(NotificationCompat.BigTextStyle().bigText("Much longer text that cannot fit one line..."))//很长的通知
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            // Set the intent that will fire when the user taps the notification
//            .setContentIntent(pendingIntent)
            .setAutoCancel(true).setOngoing(true)
        builder.setProgress(100,0,false)
        notificationManager.notify(PUSH_ID_DOWNLOAD, builder.build())
//            .setWhen()
    }

    /**
     * 检测是否有通知权限
     * @return Boolean
     */
    @JvmStatic
    fun checkPermission(): Boolean {
        return NotificationManagerCompat.from(App.mInstance).areNotificationsEnabled()
    }

    @JvmStatic
    fun openPush(context: Context) {
        context.startActivity(Intent().apply {
            action = Settings.ACTION_APP_NOTIFICATION_SETTINGS
            putExtra(Settings.EXTRA_APP_PACKAGE, context.packageName)
            putExtra(Settings.EXTRA_CHANNEL_ID, context.applicationInfo.uid)
        })
    }

    fun createNotificationChannel(): NotificationChannel {
        val name = "yc_download_name"
        val descriptionText = "yc_下载apk"
        val importance = NotificationManager.IMPORTANCE_DEFAULT
        return NotificationChannel(CHANNEL_ID_DOWNLOAD, name, importance).apply {
            description = descriptionText
            //设置提示音
//            setSound()
            //开启指示灯
//            enableLights()
            //开启震动
//            enableVibration()
            //设置锁屏展示
            lockscreenVisibility = Notification.VISIBILITY_PUBLIC
            //设置渠道描述
//            setDescription()
        }
        // Register the channel with the system
    }

}